package com.guigu.www.erp.luolujin.controller;

import com.guigu.www.erp.common.pojo.TAuthorization;
import com.guigu.www.erp.common.pojo.TRoleAuthorization;
import com.guigu.www.erp.dehao.pojo.Menu;
import com.guigu.www.erp.dehao.pojo.RoleMenu;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.luolujin.service.LljAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/aut")
public class LljAuthorServlet {
    @Autowired
    private LljAuthorService service;
    @GetMapping("/auth")
    public List<TRoleAuthorization> findAuthor(HttpServletRequest req){
        HttpSession session=req.getSession();
        User loginUser =(User) session.getAttribute("loginUser");
        int roleId=loginUser.getRole().getRoleId();
        List<TRoleAuthorization> authorizations=service.findThisAut(roleId);
        //System.out.println(authorizations);
        session.setAttribute("author",authorizations);
        return authorizations;
    }
    @GetMapping("/all/")
    public List<TAuthorization> findAuthor(){
        return service.findAll();
    }
    @GetMapping("/author/{roleId}")
    public List<TRoleAuthorization> findByIdAuthor(@PathVariable("roleId") int roleId){
        return service.findThisAut(roleId);
    }
    @DeleteMapping("{roleId}")
    public Integer delRoleAuthor(@PathVariable Integer roleId){
        service.deleteByAuthorId(roleId);
        return 0;
    }

    @PostMapping("/add")
    public int insertRoleAuthor(int roleId,int[] arr){
        for (int arra:arr) {
            TRoleAuthorization authorization=new TRoleAuthorization(null,roleId,arra,null);
            service.insertSelective(authorization);
        }
        return 1;
    }
}
